package com.peanut.baby.mvp.liveroom;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.peanut.baby.model.LiveMsg;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.tencent.mars.xlog.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/7/24.
 */

public class BaseMessageFragment extends BaseMvpFragment{
    protected static final String TAG = BaseMessageFragment.class.getSimpleName();
    @Override
    public void initData() {

    }

    protected LiveMsg getBaseMessage(int tabType, int messageType){
        LiveMsg message = new LiveMsg();
        message.tabType = tabType;
        message.messageType = messageType;
        message.roomId = getRoomId();
        return message;
    }

    private int getRoomId(){
        if(getActivity() instanceof  LiveRoomActivity){
            return ((LiveRoomActivity) getActivity()).getRoomId();
        }
        return 0;
    }

    private  void sendMessage(LiveMsg message){
        if(getActivity() instanceof  LiveRoomActivity){
            ((LiveRoomActivity) getActivity()).sendMessage(message);
        }

    }

    private String textToJsonString(String text){
        JSONObject obj = new JSONObject();
        try {
            obj.put("text",text);
        } catch (JSONException e) {
            obj = null;
            return "";
        }
        return obj.toString();
    }

    private String voiceToJsonString(String url,long duration){
        JSONObject obj = new JSONObject();
        try {
            obj.put("url",url);
            obj.put("duration",duration);
        } catch (JSONException e) {
            obj = null;
            return "";
        }
        return obj.toString();
    }

    private String imageToJsonString(String url,int width,int height){
        JSONObject obj = new JSONObject();
        try {
            obj.put("width",width);
            obj.put("height",height);
            obj.put("url",url);
        } catch (JSONException e) {
            obj = null;
            return "";
        }
        return obj.toString();
    }

    protected void sendTextMessage(int tabType,String text){
        LiveMsg message = getBaseMessage(tabType, LiveMsg.TYPE_TEXT);
        message.localMessage = textToJsonString(text);
        sendMessage(message);
    }

    protected void sendPictureMessage(int tabType,String url,String path){
        LiveMsg message = getBaseMessage(tabType, LiveMsg.TYPE_IMAGE);
        int[] size = getImageSize(path);
        message.localMessage = imageToJsonString(url,size[0],size[1]);
        sendMessage(message);
    }

    protected void sendVoiceMessage(int tabType,String url,long duration){
        LiveMsg message = getBaseMessage(tabType, LiveMsg.TYPE_VOICE);
        message.localMessage = voiceToJsonString(url,duration);
        sendMessage(message);
    }

    private int[] getImageSize(String path){
        BitmapFactory.Options options = new BitmapFactory.Options();

        /**
         * 最关键在此，把options.inJustDecodeBounds = true;
         * 这里再decodeFile()，返回的bitmap为空，但此时调用options.outHeight时，已经包含了图片的高了
         */
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回的bitmap为null
        /**
         *options.outHeight为原始图片的高
         */
        int[] size = new int[2];
        size[0] = options.outWidth;
        size[1] = options.outHeight;
        Log.d(TAG, "Bitmap size == " + options.outWidth+" "+options.outHeight);
        return size;
    }

    public void onMessageGet(List<LiveMsg> newMsgs){

    }

    /**
     *
     */
    protected void onPictureClicked(){
        if(getActivity() instanceof  LiveRoomActivity){
            ((LiveRoomActivity) getActivity()).checkPicturePermission();
        }
    }

    /**
     *
     */
    protected void onCameraClicked(){
        if(getActivity() instanceof  LiveRoomActivity){
            ((LiveRoomActivity) getActivity()).checkCameraPermission();
        }
    }

    /**
     *
     */
    protected void onVoiceClicked(){
        if(getActivity() instanceof  LiveRoomActivity){
            ((LiveRoomActivity) getActivity()).checkVoicePermission();
        }
    }

    protected void onRecordFinished(String path,long duration){
        if(getActivity() instanceof  LiveRoomActivity){
            ((LiveRoomActivity) getActivity()).uploadAudio(path,duration);
        }
    }

}
